package com.rayo.accesscontrollercopy.model.db.dao;

import com.litesuits.orm.db.assit.QueryBuilder;
import com.rayo.accesscontrollercopy.model.db.DbHelp;
import com.rayo.accesscontrollercopy.model.db.bean.KeyUserData;

import java.util.Date;
import java.util.List;

/**
 * Created by yanghaojie on 16/7/27.
 */
public class KeyUserImpl implements KeyUserDao {
    @Override
    public List<KeyUserData> findAll() {
        return DbHelp.getQueryAll(KeyUserData.class);
    }

    @Override
    public KeyUserData findByUserid(String userId) {
        List<KeyUserData> list = DbHelp.getQueryByWhere(KeyUserData.class, KeyUserData.USER_ID,
                new String[]{String.valueOf(userId)});
        if (list.size() > 0)
            return list.get(0);
        return new KeyUserData();
    }

    @Override
    public void addAll(List<KeyUserData> data) {
        DbHelp.insertAll(data);
    }

    @Override
    public Date addKeyUser(KeyUserData data) {
        QueryBuilder qb = new QueryBuilder(KeyUserData.class).whereEquals(KeyUserData.USER_ID, data.getUserid());
        List<KeyUserData> list = DbHelp.liteOrm.query(qb);

        if (null == list || list.size() == 0) {
            data.setEditime(new Date());
            DbHelp.insert(data);
            return data.getEditime();
        } else {
            KeyUserData temp = list.get(0);
            temp.setUserid(data.getUserid());
            temp.setCalendar(data.getCalendar());
            temp.setFname(data.getFname());
            temp.setBegin(data.getBegin());
            temp.setGroupid(data.getGroupid());
            temp.setGroupName(data.getGroupName());
            temp.setLname(data.getLname());
            temp.setTemplan(data.getTemplan());
            temp.setEnd(data.getEnd());
            temp.setValday(data.getValday());
            temp.setEditime(data.getEditime());

            temp.setLogin(data.getLogin());
            temp.setPassword(data.getPassword());
            DbHelp.update(temp);
            return temp.getEditime();
        }
    }

    @Override
    public void updateAll(List<KeyUserData> data) {
        DbHelp.updateALL(data);
    }


    @Override
    public void deleteAll() {
        DbHelp.deleteAll(KeyUserData.class);
    }

    @Override
    public void deleteKeyUser(KeyUserData data) {
        DbHelp.delete(data);
    }

    @Override
    public void deleteByKeyUserId(int keyId) {
        DbHelp.deleteWhere(KeyUserData.class, KeyUserData.USER_ID, new String[]{String.valueOf(keyId)});
    }

    @Override
    public KeyUserData findByLogin(String login) {
        List<KeyUserData> list = DbHelp.getQueryByWhere(KeyUserData.class,"login",
                new String[]{login});
        if (list.size() > 0)
            return list.get(0);
        return null;
    }

    @Override
    public void update(KeyUserData keyUserData) {
        DbHelp.update(keyUserData);
    }
}

